<template>
	<view class="adress-container">
      <view class="adress-btn" v-if="JSON.stringify(address) === '{}' ">
        <button type="primary" size="mini" @click="chooseAddress"> 请选择送货地址+ </button>
      </view>
    
    <view class="adress-info" @click="chooseAddress" v-else>
      <view class="user-ph">
        <view class="user">
          收货人：{{address.userName}}
        </view>
        <view class="ph">
          <view class="">
            电话：{{address.telNumber}}
          </view>
          <view class="">
            <uni-icons type="arrowright"></uni-icons>`
          </view>
        </view>
      </view>
      <view class="adress">
        收货地址：{{addrs}}
      </view>
    </view>
    
    
    <!-- 底部的边框线 -->
    <image src="/static/cart_border@2x.png" class="bottom-border"></image>
	</view>
</template>

<script>
  import {mapMutations,mapGetters,mapState} from 'vuex'
	export default {
		data() {
			return {
			}
		},
    computed:{
      ...mapGetters('m_user',['addrs']),
      ...mapState('m_user',['address'])
    },
    methods:{
      ...mapMutations('m_user',['updateAddress']),
      async chooseAddress(){
        const [err,succ] = await uni.chooseAddress().catch(err => err)
        console.log(succ,'succ')
        if(succ && succ.errMsg === 'chooseAddress:ok'){
          this.updateAddress(succ)
        }
        // 如果没有用户授权
        if(err && (err.Msg === 'chooseAdress === fail auth deny' || 'chooseAddress:fail authorize no response') )
        this.reAuth() //调用方法重新向用户发起授权申请
      },
      
      // 向用户授权
    async reAuth(){
      // 提示用户对地址进行授权
      const [err2, confirmResult] = await uni.showModal({
        content:'检测到您没打开地址授权，是否区设置打开？',
        confirmText:'确认',
        cancelText:'取消'
      })
      
      // 如果弹框异常,直接推出
      if(err2) return
        
        // 如果点击取消
        if(confirmResult.cancel) return uni.$showMsg('您取消了地址授权！')
        
        // 如果点击确认
        if(confirmRsult.cancel) return uni.openSetting({
          success:(settingResult) => {
                  // 3.4.2 地址授权的值等于 true，提示用户 “授权成功”
                  if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
                  // 3.4.3 地址授权的值等于 false，提示用户 “您取消了地址授权”
                  if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！')
          }
        })
        
    }
    
    }
	}
</script>

<style lang="scss">
.adress-container{
  .adress-btn{
    padding: 10px 0;
    text-align: center;
  }
  
  .adress-info{
    font-size: 14px;
    margin: 10px 0 0 10px;
    .user-ph{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .ph{
        display: flex;
        justify-content: space-between;
      }
    }
  }
  
  .bottom-border{
    width: 100%;
    height: 6px;
  }
}
</style>
